package org.dtp.autoconfigure;

import org.dtp.DynamicThreadPoolManager;
import org.dtp.alarm.DynamicThreadPoolAlarm;
import org.dtp.config.DynamicThreadPoolProperties;
import org.dtp.endpoint.DynamicThreadPoolEndpoint;
import org.dtp.listener.ApolloConfigUpdateListener;
import org.dtp.listener.NacosCloudConfigUpdateListener;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName DynamicThreadPoolAutoConfiguration
 * @Description 动态线程池
 * @Author wangyiqi
 * @Date 2021-08-13-19:55
 **/
@ImportAutoConfiguration(DynamicThreadPoolProperties.class)
@Configuration
public class DynamicThreadPoolAutoConfiguration {

    /**
     * 动态线程池管理
     *
     * @return /
     */
    @Bean
    public DynamicThreadPoolManager dynamicThreadPoolManager() {
        return new DynamicThreadPoolManager();
    }

    /**
     * 端点
     *
     * @return /
     */
    @Bean
    public DynamicThreadPoolEndpoint threadPoolEndpoint() {
        return new DynamicThreadPoolEndpoint();
    }

    /**
     * 线程池告警
     *
     * @return /
     */
    @Bean
    @ConditionalOnProperty(value = "dynamic.threadpools.alarm.enabled", matchIfMissing = true)
    public DynamicThreadPoolAlarm dynamicThreadPoolAlarm() {
        return new DynamicThreadPoolAlarm();
    }

    /**
     * apollo监听
     */
    @Configuration
    @ConditionalOnClass(value = com.ctrip.framework.apollo.ConfigService.class)
    protected static class ApolloConfiguration {

        @Bean
        public ApolloConfigUpdateListener apolloConfigUpdateListener() {
            return new ApolloConfigUpdateListener();
        }

    }

    /**
     * nacos监听
     */
    @Configuration
    @ConditionalOnClass(value = com.alibaba.cloud.nacos.NacosConfigProperties.class)
    protected static class NacosCloudConfiguration {

        @Bean
        public NacosCloudConfigUpdateListener nacosCloudConfigUpdateListener() {
            return new NacosCloudConfigUpdateListener();
        }

    }
}
